package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.entityVo.CollectionVo;
import com.antdesignbackend.backend.service.CollectionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/collection")
public class CollectionController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    private Integer menuId;
    private Integer userId;
    private Integer userType;
    private String roleID;
    private Integer shopId;
    @Autowired
    private CollectionService collectionService;

    /**
     * 收藏列表首页
     */
    @GetMapping("/index")
    @AuthToken
    public String index(String menuId, HttpServletRequest request) {
        this.menuId = Integer.parseInt(menuId);
        return "collection/index";

    }
    @GetMapping("/index2/{shopId}")
    @AuthToken
    public String index2(HttpServletRequest request,@PathVariable int shopId) {
        this.shopId=shopId;
        return "collection/index2";
    }
    /***
     * 前端分页查询
     *
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch(@RequestBody CollectionVo data, HttpServletRequest request) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        result = new ReturnVO();
        List<CollectionVo> collectionVos=new ArrayList<>();
        QueryWrapper<CollectionVo> wrapper =new QueryWrapper<>();
        //正常查询过滤条件
        if(data.getShopName()!=null && !data.getShopName().equals(""))
        {
            System.out.println("前端传来的店铺名称为"+data.getShopName());
//           wrapper.having("shopName like ‘%"+data.getShopName()+"%’");
//            wrapper.having("shopName like '%{0}%'",data.getShopName());
//            wrapper.apply("having shopName like ‘%"+data.getShopName()+"%’");
            wrapper.like("shopName",data.getShopName());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=collectionService.sumCount(wrapper);
        System.out.println("收藏人数的信息条数有---------------"+total);
        result.setTotal(total);
        //再查询
        if(total>0)
        {
            collectionVos=collectionService.sum(wrapper,new Page<>(data.getPageIndex(),data.getPageSize()));
            System.out.println("收藏人数的信息有---------------"+collectionVos);
        }
        result.setData(collectionVos);
        return result;
    }
    @PostMapping("/tableSearch2")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch2(@RequestBody CollectionVo data, HttpServletRequest request) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        result = new ReturnVO();
        List<CollectionVo> collectionVos=new ArrayList<>();
        QueryWrapper<CollectionVo> wrapper =new QueryWrapper<>();
        wrapper.eq("ShopID",shopId);
        //正常查询过滤条件
        if(data.getUserName()!=null && !data.getUserName().equals(""))
        {
            wrapper.like("userName",data.getUserName());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=collectionService.count2(wrapper);
        System.out.println("收藏人数的信息条数有---------------"+total);
        result.setTotal(total);
        //再查询
        if(total>0)
        {
            collectionVos=collectionService.search2(wrapper,new Page<>(data.getPageIndex(),data.getPageSize()));
            System.out.println("收藏人数的信息有---------------"+collectionVos);
        }
        result.setData(collectionVos);
        return result;
    }
}
